package net.aaa.hotel.controller;

import com.alibaba.fastjson.JSON;

import net.aaa.hotel.dao.T_sheshiDao;
import net.aaa.hotel.dao.impl.T_sheshiDaoImpl;
import net.aaa.hotel.entity.PageInfo;
import net.aaa.hotel.entity.T_sheshi;
import net.aaa.hotel.util.LayuiTable;
import net.aaa.hotel.util.TransBean;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 凉凉
 * @time ${datetime}
 */
@WebServlet("/T_sheshiServlet")
public class T_sheshiServlet extends HttpServlet {
    T_sheshiDao t_sheshiDao = new T_sheshiDaoImpl();
    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String method = req.getParameter("method");
        if (method!=null){
            switch (method){
                case "del":
                    delete(req,resp);
                    break;
                case "upd":
                    update(req,resp);
                    break;
                case "add":
                    add(req,resp);
                    break;
                case "showTable":
                    showTable(req,resp);
                    break;
            }
        }else {

            //查询出所有的客户的信息
//            List<Map> maps = t_sheshiDao.findAll();
            //把这个数据转化为json数据  fastjson
            // String s = JSON.toJSONString(maps);//[{},{}]
//            Map m=new HashMap();
//            m.put("code",0);
//            m.put("msg","");
//            m.put("count",maps.size());
//            m.put("data",maps);
            //
//            String map= JSON.toJSONString(m);
            //把这个数据传递到页面  out
//            PrintWriter out = resp.getWriter();
//            out.print(map);
//            out.flush();
//            out.close();

            //req.setAttribute("glist", maps);
            //携带页面 请求转发   重定向
            // req.getRequestDispatcher("index.jsp").forward(req, resp);


          /*  List<Map> sheshi = t_sheshiDao.findAll();
            req.setAttribute("ss",sheshi);
            req.getRequestDispatcher("jsp/sheshi.jsp").forward(req,resp);*/
        }
    }


    
    private void showTable(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PageInfo pageInfo = new PageInfo();
        String cu = req.getParameter("curr");
        String nu = req.getParameter("nums");
        String condition = req.getParameter("condition");
//        System.out.println("condition+++++++"+condition);
//        System.out.println("curr+++++"+cu);
//        System.out.println("nums+++++"+nu);
        int curr = Integer.parseInt(cu);
        int nums = Integer.parseInt(nu);
        int count = t_sheshiDao.getCount(condition);
        List<Map> allByPage = t_sheshiDao.getAllByPage(curr, nums,condition);
//        System.out.println("curr+++++"+curr);
//        System.out.println("nums+++++"+nums);
//        System.out.println("allBypage++++++"+allByPage);
        LayuiTable layuiTable = new LayuiTable(count, allByPage);
        String string = JSON.toJSONString(layuiTable);
        PrintWriter out = resp.getWriter();
        out.print(string);
        out.flush();
        out.close();
    }











    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ssId = req.getParameter("ssId");
        int delete = t_sheshiDao.delete(ssId);
        if (delete>0){
            resp.sendRedirect("sheshi/sheshi.jsp");
        }
    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ssId = req.getParameter("ssId");
        Map byid = t_sheshiDao.findByid(ssId);
        req.setAttribute("ssId",byid);
        req.getRequestDispatcher("T_sheshiServlet?method=add").forward(req,resp);
    }
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        T_sheshi t_sheshi=new T_sheshi();
        TransBean.populate(t_sheshi,parameterMap);
//        System.out.println(t_sheshi.toString());
        int add = t_sheshiDao.add(t_sheshi);
//        System.out.println(add);
        PrintWriter writer = resp.getWriter();
        writer.print(add);
        writer.flush();
        writer.close();
    }



//    public LayuiTable showTable(Integer curr,Integer nums){
//        int count = t_sheshiDao.getCount();
//        List<Map> allByPage = t_sheshiDao.getAllByPage(curr, nums);
//        System.out.println(allByPage);
//        LayuiTable layuiTable = new LayuiTable(count, allByPage);
//        return layuiTable;
//    }

}
